<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        a {
            text-decoration: none;
            color: inherit;
        }

        li {
            list-style: none;
        }

        img {
            vertical-align: top;
        }

        #showGoods {
            width: 1226px;
            margin: 0 auto;
            margin-top: 50px;
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
        }

        #showGoods li {
            width: 18%;
            box-sizing: border-box;
            padding: 10px;
            font-size: 13px;
            line-height: 20px;
        }

        #showGoods li:hover {
            box-shadow: 0 0 1px 2px rgba(0, 0, 0, 0.2);
        }

        #showGoods li img {
            width: 70%;
            display: block;
            margin: 20px auto;
        }

        #showGoods li .price {
            color: red;
        }

        #showGoods li .name {
            /* 多行文本溢出隐藏 */
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    </style>
</head>

<body>
    <ul id="showGoods">
        <!-- <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/195641/2/30490/89452/63745247Efb2a8608/46028f28c82c3faf.jpg"
                    alt="">
                <div class="price">6499.00</div>
                <div class="name">华为（HUAWEI） 笔记本电脑MateBook D16 16英寸高性能轻薄本商务办公学生手提超极本 银丨十二代i5-12500H 16G 512G</div>
                <div class="comment">2000+条评价</div>
                <div class="shop">华为比特专卖店</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/195641/2/30490/89452/63745247Efb2a8608/46028f28c82c3faf.jpg"
                    alt="">
                <div class="price">6499.00</div>
                <div class="name">华为（HUAWEI） 笔记本电脑MateBook D16 16英寸高性能轻薄本商务办公学生手提超极本 银丨十二代i5-12500H 16G 512G</div>
                <div class="comment">2000+条评价</div>
                <div class="shop">华为比特专卖店</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/195641/2/30490/89452/63745247Efb2a8608/46028f28c82c3faf.jpg"
                    alt="">
                <div class="price">6499.00</div>
                <div class="name">华为（HUAWEI） 笔记本电脑MateBook D16 16英寸高性能轻薄本商务办公学生手提超极本 银丨十二代i5-12500H 16G 512G</div>
                <div class="comment">2000+条评价</div>
                <div class="shop">华为比特专卖店</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/195641/2/30490/89452/63745247Efb2a8608/46028f28c82c3faf.jpg"
                    alt="">
                <div class="price">6499.00</div>
                <div class="name">华为（HUAWEI） 笔记本电脑MateBook D16 16英寸高性能轻薄本商务办公学生手提超极本 银丨十二代i5-12500H 16G 512G</div>
                <div class="comment">2000+条评价</div>
                <div class="shop">华为比特专卖店</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/195641/2/30490/89452/63745247Efb2a8608/46028f28c82c3faf.jpg"
                    alt="">
                <div class="price">6499.00</div>
                <div class="name">华为（HUAWEI） 笔记本电脑MateBook D16 16英寸高性能轻薄本商务办公学生手提超极本 银丨十二代i5-12500H 16G 512G</div>
                <div class="comment">2000+条评价</div>
                <div class="shop">华为比特专卖店</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/195641/2/30490/89452/63745247Efb2a8608/46028f28c82c3faf.jpg"
                    alt="">
                <div class="price">6499.00</div>
                <div class="name">华为（HUAWEI） 笔记本电脑MateBook D16 16英寸高性能轻薄本商务办公学生手提超极本 银丨十二代i5-12500H 16G 512G</div>
                <div class="comment">2000+条评价</div>
                <div class="shop">华为比特专卖店</div>
            </a>
        </li> -->
    </ul>
</body>
<script>

    // 动态生成之前先写静态页面 

    var showGoods = document.getElementById("showGoods");

    var xhr = new XMLHttpRequest();

    xhr.open("get", "../data/goods.json", true);

    xhr.send();

    xhr.onreadystatechange = function () {
        // 2 3 4
        if (xhr.readyState == 4 && xhr.status == 200) {
            var result = xhr.responseText;
            // console.log("请求成功", result);

            result = JSON.parse(result);
            console.log(result);

            // code后端接口返回的信息  -> 请求成功 失败 
            var { code, message, list } = result;

            if (code) {
                var html = "";
                // list.forEach(function (item) {

                //     var { goodsName, goodsImg, goodsPrice, commentCount, shopName } = item;

                //     html += `<li>
                //         <a href="">
                //             <img src="${goodsImg}"
                //                 alt="">
                //             <div class="price">${goodsPrice}</div>
                //             <div class="name">${goodsName}</div>
                //             <div class="comment">${commentCount}</div>
                //             <div class="shop">${shopName}</div>
                //         </a>
                //     </li>`;
                // });

                // list = [ {},{},{},{}]

                // 函数的参数解构:  如果已经确定函数的参数是数组和对象  => 可以对该参数直接进行解构
                // list.forEach(({ goodsName, goodsImg, goodsPrice, commentCount, shopName } = item) => {

                //     html += `<li>
                //         <a href="">
                //             <img src="${goodsImg}"
                //                 alt="">
                //             <div class="price">${goodsPrice}</div>
                //             <div class="name">${goodsName}</div>
                //             <div class="comment">${commentCount}</div>
                //             <div class="shop">${shopName}</div>
                //         </a>
                //     </li>`;
                // });

                list.forEach(({ goodsName, goodsImg, goodsPrice, commentCount, shopName }) => {
                    html += `<li>
                        <a href="">
                            <img src="${goodsImg}"
                                alt="">
                            <div class="price">${goodsPrice}</div>
                            <div class="name">${goodsName}</div>
                            <div class="comment">${commentCount}</div>
                            <div class="shop">${shopName}</div>
                        </a>
                    </li>`;
                });

                showGoods.innerHTML = html;
            }


        }
    }








</script>

</html>